package io.bluerain.fragment.ui;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import io.bluerain.fragment.ui.entities.BookContent;

/**
 * Created by foredawn on 15-9-21.
 * ListFragment基本用法
 */
public class BookListFragment extends ListFragment {

    private Callback callback;

    public interface Callback {
        void onItemSelected(Integer id);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1, BookContent.ITEMS));
    }

    @Override //添加显示
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        callback = (Callback) activity;
    }

    @Override //删除时
    public void onDetach() {
        super.onDetach();
        callback = null;
    }

    @Override //单击列表项
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //触发回调函数
        callback.onItemSelected(BookContent.ITEMS_MAP.get(position).id);
    }
}
